layui.config({
    base: '/static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'table'], function () {
    var $ = layui.$
        , form = layui.form
        , table = layui.table;

    //监听搜索
    form.on('submit(LAY-user-back-search)', function (data) {
        var field = data.field;

        //执行重载
        table.reload('menu-list', {
            where: field
        });
        return false;
    });

    table.render({
        elem: "#menu-list",
        height: "",
        url: "/menu/pageList",
        title: "菜单列表",
        page: page(),
        toolbar: "#toolbar",
        totalRow: false,
        response: response(),
        parseData: function (res) {
            var dataWrapper = res.dataWrapper;
            if (isNotNull(dataWrapper.page)) {
                return {
                    "status": res.status,
                    "msg": res.msg,
                    "total": dataWrapper.page.total,
                    "data": dataWrapper.page.list
                }
            }
        },
        cols: [[
            {type: "checkbox", fixed: "left"},
            {type: "numbers", title: "序号"},
            {field: "name", title: "菜单名称", sort: true},
            {field: "path", title: "菜单路径"},
            {fixed: 'right', title: "操作", width: 150, align: "center", toolbar: "#columnToolbar"}
        ]],
        done: function (data) {
            var href = data.sourceUrl;
            if (data.status === 1 && isNotNull()) {
                window.location.href = href;
            }
        }
    });

    //监听头工具栏事件
    table.on("toolbar(menu-list)", function (obj) {
        var checkStatus = table.checkStatus(obj.config.id),
            data = checkStatus.data;
        switch (obj.event) {
            case "add":
                layer.open({
                    type: 1,
                    title: "添加菜单",
                    content: $("#addMenu"),
                    area: ['350px', '250px'],
                    btn: ['确定', '重置', '取消'],
                    yes: function (index) {
                        addMenu(index);
                    }, btn2: function () {
                        $("#name").val("");
                        $("#path").val("");
                        return false;
                    }
                });
                break;
            case "update":
                if (data.length === 0) {
                    layer.msg("请选择一行");
                } else if (data.length > 1) {
                    layer.msg("只能同事编辑一行");
                } else {
                    layer.alert("编辑[id]:" + checkStatus.data[0].id);
                }
                break;
            case "batchDel":
                if (data.length === 0) {
                    layer.msg("请选择一行", {icon: 2}, "2");
                } else {
                    layer.confirm("确认删除吗？", {icon: 3}, function (index) {
                        var array = [];
                        data.forEach(function (item) {
                            array.push(item.id);
                        });
                        var postData = {idArray: array};
                        deleteMenu(postData);
                    });
                }
                break;
        }
    });

    function addMenu(index) {
        var formData = {
            name: $("#name").val(),
            path: $("#path").val()
        };
        if (isNull(formData.name)) {
            layer.msg("请输入菜单名", {icon: 2});
            return;
        }
        if (isNull(formData.path)) {
            layer.msg("请输入菜单路径", {icon: 2});
            return;
        }
        $.ajax({
            url: '/menu/add',
            type: 'POST',
            data: formData,
            success: function (result) {
                if (result.status === 0) {
                    table.reload('menu-list');
                    layer.close(index);
                    layer.msg(result.msg, {icon: 1})
                } else {
                    layer.msg(result.msg, {icon: 2});
                }
            }, error: function () {
                layer.msg("网络错误")
            }
        });
    }

    function updateMenu(index, id) {
        var formData = {
            id: id,
            name: $("#name").val(),
            path: $("#path").val()
        };
        if (isNull(formData.name)) {
            layer.msg("请输入菜单名", {icon: 2});
            return;
        }
        if (isNull(formData.path)) {
            layer.msg("请输入菜单路径", {icon: 2});
            return;
        }
        $.ajax({
            url: '/menu/update',
            type: 'POST',
            data: formData,
            success: function (result) {
                if (result.status === 0) {
                    table.reload('menu-list');
                    layer.close(index);
                    layer.msg(result.msg, {icon: 1})
                } else {
                    layer.msg(result.msg, {icon: 2});
                }
            }, error: function () {
                layer.msg("网络错误")
            }
        });
    }

    function deleteMenu(postData) {
        $.ajax({
            url: "/menu/delete",
            type: "POST",
            data: postData,
            traditional: true,
            beforeSend: function () {
                table.reload('menu-list');
                layer.close(index);
            }, success: function (res) {
                if (res.status === 0) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, error: function () {
                layer.msg("网络错误", {icon: 2})
            }
        })
    }

    //监听行工具事件
    table.on("tool(menu-list)", function (obj) {
        var data = obj.data,
            layEvent = obj.event;
        if (layEvent === 'del') {
            layer.confirm("确定要删除吗？", function (index) {
                var postData = {idArray: data.id};
                deleteMenu(postData);
                layer.close(index);
            });
        } else if (layEvent === 'edit') {
            $("#name").val(data.name);
            $("#path").val(data.path);
            layer.open({
                type: 1,
                title: "添加菜单",
                content: $("#addMenu"),
                area: ['350px', '250px'],
                btn: ['确定', '取消'],
                yes: function (index) {
                    updateMenu(index, data.id);
                }
            });
        }
    });
});